package api

import (
	"encoding/json"
	"gitee.com/gopher2011/gva/server/dao"
	"github.com/gin-gonic/gin"
)

// sysOperationRecordApi 管理对象。
var SysOperationRecord = &sysOperationRecordApi{}

type sysOperationRecordApi struct{}

// @Summary 创建SysOperationRecord
// @Router /sysOperationRecord/createSysOperationRecord [post]
func (it *sysOperationRecordApi) CreateSysOperationRecord(r *gin.Context) {
	// 将请求参数解析到 ptr上
	var ptr dao.SysOperationRecord
	if err := r.ShouldBind(&ptr);err !=nil {
		FailWithMessage(r,err.Error())
		return
	}
	if rows := dao.SysOperationRecordDao.Insert(ptr);rows <= 0 {
		dao.Log.Error("创建失败!")
		FailWithMessage(r,"创建失败")
		return
	}
	OkWithMessage(r,"创建成功")
}

// @Summary 删除SysOperationRecord
// @Router /sysOperationRecord/deleteSysOperationRecord [delete]
func (it *sysOperationRecordApi) DeleteSysOperationRecord(r *gin.Context) {
	var ptr dao.SysOperationRecord
	if err := r.ShouldBind(&ptr); err != nil {
		dao.Log.Error("删除失败!")
		FailWithMessage(r,"删除失败!")
		return
	}
	if rows := dao.SysOperationRecordDao.Delete(ptr); rows <=0 {
		dao.Log.Error("删除失败!")
		FailWithMessage(r,"删除失败!")
		return
	}
	OkWithMessage(r,"删除成功")
}

// @Summary 批量删除SysOperationRecord
// @Router /sysOperationRecord/deleteSysOperationRecordByIds [delete]
func (it *sysOperationRecordApi) DeleteSysOperationRecordByIds(r *gin.Context) {
	var ptr dao.IdsReq
	if err := r.ShouldBind(&ptr); err != nil {
		FailWithMessage(r,err.Error())
		return
	}
	if rows := dao.SysOperationRecordDao.DeleteByIds(ptr.Ids); rows <= 0 {
		dao.Log.Error("批量删除失败!")
		FailWithMessage(r,"批量删除失败")
		return
	}
	OkWithMessage(r,"批量删除成功!")
}

// @Summary 用id查询SysOperationRecord
// @Router /sysOperationRecord/findSysOperationRecord [get]
func (it *sysOperationRecordApi)FindSysOperationRecord(r *gin.Context) {
	var ptr dao.SysOperationRecord
	if err := r.ShouldBind(&ptr);err != nil {
		FailWithMessage(r,err.Error())
		return
	}
	if obj := dao.SysOperationRecordDao.Select(ptr.Id); obj.Id <=0 {
		dao.Log.Error("查询失败!")
		FailWithMessage(r,"查询失败!")
	}else {
		OkWithDetailed(r,"查询成功",gin.H{"resysOperationRecord":obj})
	}
}
// @Summary 分页获取SysOperationRecord列表
// @Router /sysOperationRecord/getSysOperationRecordList [get]
func (it *sysOperationRecordApi)GetSysOperationRecordList(r *gin.Context) {
	var ptr dao.SysOperationRecordSearch
	if err := r.ShouldBind(&ptr); err != nil {
		FailWithMessage(r,err.Error())
		return
	}
	var obj []dao.SysOperationRecord
	ptr.PageSize = ptr.PageSize * (ptr.Page -1)
	list := dao.SysOperationRecordDao.List(ptr)
	marshal, _ := json.Marshal(list)
	json.Unmarshal(marshal,&obj)
	total := len(list)
	if total <= 0 {
		dao.Log.Error("获取失败!")
		FailWithMessage(r,"获取失败!")
		return
	}
	OkWithDetailed(r,"获取成功",dao.PageResult{
		List: obj,
		Total: total,
		Page:ptr.Page,
		PageSize: ptr.PageSize,
	})
}
